﻿using NSJL.Framework.Utils;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DJ.ZEF.ThirdParty;
using NSJL.Biz.Background.Client;
using DJ.Dialog;
using DJ.Clients;
using NSJL.DAL.DataModel.Entities;

namespace DJ.Pages
{
    /// <summary>
    /// SetBoxPage.xaml 的交互逻辑
    /// </summary>
    public partial class SetBoxPage : Page
    {
        //初始化数据库
        CommBiz biz = new CommBiz();
        public Action action;
        public SetBoxPage()
        {
            InitializeComponent();
        }
        public IniFile ini = new IniFile("Config/Config.ini");
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var p0 = TextBox1_Copy.Text;
                var p1 = TextBox1.Text.ToInt32();
                var p2 = ComboBox1.SelectionBoxItem.ToString();
                var p3 = ComboBox2.SelectionBoxItem.ToString();
                //ini.writeKey("BoxConfig", "count", p1.ToString());
                //ini.writeKey("BoxConfig", "portName", p2);
                //ini.writeKey("BoxConfig", "baudRate", p3);
                //ini.writeKey("BoxConfig", "cabinetName",p0);
                
     
                DevicesInfo devicesInfo = biz.GetDevicesInfo("锁板").FirstOrDefault();
                if (devicesInfo != null)
                {
                    biz.EditDevicesInfo(devicesInfo.DeviceName, p2, p3, "0", devicesInfo.Id, p1);
                }
                else
                {
                    biz.EditDevicesInfo("锁板", p2, p3, "0",null,p1);
                }
                
                var isca=biz.IsCabinet(p0);
                if (!isca)
                {
                    MessageDialog.ShowDialog("柜号名不存在，请先添加柜号");
                    return;
                }

                biz.InitBoxInfo(p1, p0);

                LockManager.GetInstance().Close();
                LockManager.GetInstance().Start(p2, p3.ToInt32());

                MessageDialog.ShowDialog("操作成功");

                action?.Invoke();
            }
            catch (Exception ex)
            {
                MessageDialog.ShowDialog(ex.Message);
            }
            
        }

        private void TextBox1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ComWindow.Focus(sender);
            e.Handled = true;
        }

        private void Page_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.IsVisible)
            {
                DevicesInfo devicesInfo = biz.GetDevicesInfo("锁板").FirstOrDefault();
                if (devicesInfo != null)
                {
                    TextBox1.Text = devicesInfo.Count.ToString();
                    TextBox1_Copy.Text = devicesInfo.CabName.ToString();
                    foreach (ComboBoxItem item in ComboBox1.Items)
                    {
                        if ((string)item.Content == devicesInfo.COM.ToString())
                        {
                            item.IsSelected = true;
                        }
                        else
                        {
                            item.IsSelected = false;
                        }
                    }
                    foreach (ComboBoxItem item in ComboBox2.Items)
                    {
                        if ((string)item.Content == devicesInfo.Baud.ToString())
                        {
                            item.IsSelected = true;

                        }
                        else
                        {
                            item.IsSelected = false;
                        }
                    }
                }
            }
        }
    }
}
